//
//  MovieTableViewCell.m
//  Movie
//
//  Created by cjy on 16/3/23.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "MovieTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "MovieModel.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@implementation MovieTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
    self.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    self.clipsToBounds = YES;
    
    //图片
    _picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, -(kHeight/1.7 - 200)/2, kWidth, kHeight/1.7)];
    
    _picture.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView  addSubview:_picture];
    
    
    // view
    _coverview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    _coverview.backgroundColor = [UIColor colorWithWhite:0 alpha:0.33];
        
    [self.contentView addSubview:_coverview];
    
        
    // 标题
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 / 2 - 30, kWidth, 30)];
    _titleLabel.font = [UIFont boldSystemFontOfSize:16];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor whiteColor];
    
    [self.contentView addSubview:_titleLabel];
    
    // 副标题
    _littleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 / 2 + 30, kWidth, 30)];
    _littleLabel.font = [UIFont systemFontOfSize:14];
    _littleLabel.textAlignment = NSTextAlignmentCenter;
    _littleLabel.textColor = [UIColor whiteColor];
    
    [self.contentView addSubview:_littleLabel];
        

    }
    return self;

}

// 传值
- (void)setModel:(MovieModel *)model{
    
    
    if (_model != model) {
        
        [_picture sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail] placeholderImage:nil];
        
        _titleLabel.text = model.title;
   
        // 转换时间
        NSInteger time = model.duration;
        
        NSString *timeString = [NSString stringWithFormat:@"%02ld'%02ld''",time/60,time% 60];//显示的是音乐的总时间
        
        NSString *string = [NSString stringWithFormat:@"#%@ / %@",model.category, timeString];
        
        _littleLabel.text = string;
    }
    
    
}

// cell 的偏移量
- (CGFloat)cellOffset {
    
    CGRect centerToWindow = [self convertRect:self.bounds toView:self.window];
    CGFloat centerY = CGRectGetMidY(centerToWindow);
    CGPoint windowCenter = self.superview.center;
    
    CGFloat cellOffsetY = centerY - windowCenter.y;
    
    CGFloat offsetDig =  cellOffsetY / self.superview.frame.size.height *2;
    CGFloat offset =  -offsetDig * (kHeight/1.7 - 200)/2;
    
    CGAffineTransform transY = CGAffineTransformMakeTranslation(0,offset);
    
    
    
    self.picture.transform = transY;
    
    return offset;
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
